package cn.com.bluemoon.daps.standard.mapper;

import cn.com.bluemoon.daps.standard.dto.StandardQueryDto;
import cn.com.bluemoon.daps.standard.entity.DapStandardIndex;
import cn.com.bluemoon.daps.standard.excel.pojo.IndexStandardExportDto;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 指标标准表 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Repository
public interface DapStandardIndexMapper extends BaseMapper<DapStandardIndex> {

    /**
     * 根据标准唯一id更新发布信息
     *
     * @param standardUniqueId 同一标准的唯一id
     * @param bool             修改 字段publish_newest
     */
    @Update("update dap_standard_index set update_time=current_timestamp(), publish_newest=#{bool} " +
            " where standard_unique_id =#{standardUniqueId} and publish_newest <> #{bool} and bm_status=1 ")
    void updatePublishNewestByStandardUniqueId(@Param("standardUniqueId") String standardUniqueId, @Param("bool") Boolean bool);

    @Select("select * from dap_standard_index\n" +
            "where standard_unique_id <> #{standardUniqueId} " +
            "and bm_status=1 " +
            "and FIND_IN_SET(#{standardUniqueId}, related_index) > 0")
    List<DapStandardIndex> findOtherRelateIndexes(@Param("standardUniqueId") String standardUniqueId);

    @Select("select e.code_name                 as topic,\n" +
            "       b.code_name                 as category1,\n" +
            "       c.code_name                 as category2,\n" +
            "       d.code_name                 as category3,\n" +
            "       a.index_chin_name           as indexName,\n" +
            "       a.index_eng_name            as engName,\n" +
            "       f.code_name                 as bizLine,\n" +
            "       a.index_biz_desc            as bizDesc,\n" +
            "       a.index_app_name            as appName,\n" +
            "       a.business_definition       as bizDef,\n" +
            "       a.index_usage               as indexUsage,\n" +
            "       g.name                      as dsSys,\n" +
            "       a.access_logic              as fetchLogic,\n" +
            "       a.data_warehouse_make_logic as whProcessLogic,\n" +
            "       a.data_warehouse_model      as whModel, -- d_ec_oms_order_dtl.tot_order_price 来源于data_warehouse_model\n" +
            "       --     a. as whModelTable, -- 来源于data_warehouse_model\n" +
            "       h.depart_name               as manageDept,\n" +
            "       a.business_personnel        as bizPerson,\n" +
            "       a.data_analyst              as dataAnalyzer,\n" +
            "       a.data_warehouse_developer  as whDevPerson,\n" +
            "       case a.index_type\n" +
            "           when 1 then '原子指标'\n" +
            "           when 2 then '派生指标'\n" +
            "           when 3 then '计算指标'\n" +
            "           end                     as indexType,\n" +
            "       case a.index_format\n" +
            "           when 3 then '金额类'\n" +
            "           when 5 then '数值类'\n" +
            "           end                     as indexDataStyle,\n" +
            "       a.common_units              as unit,\n" +
            "       a.data_accuracy             as fetchPrecision,\n" +
            "       a.related_index             as relateIndexIds,\n" +
            "       a.association_dimension     as relateDimIds,\n" +
            "       a.value_range               as fetchRange,\n" +
            "       a.date_length               as dataLength,\n" +
            "       a.id                        as standardId,\n" +
            "       a.standard_unique_id        as standardUniqueId\n" +
            "from dap_standard_index a\n" +
            "         left join dap_system_category b on a.primary_sort = b.id\n" +
            "         left join dap_system_category c on a.secondary_sort = c.id\n" +
            "         left join dap_system_category d on a.third_sort = d.id\n" +
            "         left join dap_system_category e on a.theme = e.id\n" +
            "         left join dap_system_biz_line f on a.biz_line_id = f.biz_line_id\n" +
            "         left join dap_system_department h on a.management_department_id = h.id\n" +
            "         left join dap_system_info g on a.data_source_system = g.id\n" +
            "where a.publish_newest = 1\n" +
            "  and a.bm_status = 1 \n")
    List<IndexStandardExportDto> queryAllPublishIndexStandard();

    List<IndexStandardExportDto> queryPublishIndexStandard(@Param("queryVo") StandardQueryDto queryVo);
}
